package com.example.common.network

import com.example.base.base.application.BaseApplication
import com.example.common.constant.Contents
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

/**
 * @Description
 * @department 城市元宇宙组
 * @Author niushilong
 * @Date 2024-04-01 11:16
 */
object Api {

    val okHttpClient by lazy {
        OkHttpClient.Builder()
            .cache(
                Cache(
                    File(BaseApplication.applicationContext().cacheDir, "http_cache"),
                    50L * 1024L * 1024
                )
            )
            .connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .writeTimeout(5, TimeUnit.SECONDS)
            .connectTimeout(10, TimeUnit.SECONDS)
            .build()
    }

    val retrofitClient by lazy {
        Retrofit.Builder()
            .baseUrl(Contents.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    fun <T> getService(service: Class<T>): T{
        return retrofitClient.create(service)
    }
}